package com.jrh.biz.siteMsg;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jrh.biz.user.UserService;
import com.jrh.common.condition.Precondition;
import com.jrh.common.exception.DataNotFoundException;
import com.jrh.dal.SiteMsgDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.SiteMsg;
import com.jrh.domain.entity.User;

/**
 * 类SiteMsgServiceImpl.java的实现描述：TODO 类实现描述
 * 
 * @author mmyjse@gmail.com 2012-8-11 上午11:27:01
 */
public class SiteMsgServiceImpl implements SiteMsgService {

    private static final Logger log = LoggerFactory.getLogger(SiteMsgServiceImpl.class);

    private SiteMsgDao          siteMsgDao;

    private UserService         userService;

    @Override
    public boolean sendSiteMsg(SiteMsg siteMsg) throws DataNotFoundException {
        Precondition.checkNotNull(siteMsg, "siteMsg can not null!");
        Precondition.checkNotNull(siteMsg.getReceiverLoginId(), "receiverLoginId can not null!");
        User user = userService.getUserByLoginIdAndSite(siteMsg.getReceiverLoginId(), null);
        if (user == null) {
            throw new DataNotFoundException(DataNotFoundException.ERROR_CODE_USER, "user not found.");
        }
        if (StringUtils.isBlank(siteMsg.getCntType())) {
            siteMsg.setCntType("0");
        }
        siteMsg.setIsRead(false);
        siteMsg.setSendDate(new Date());
        try {
            siteMsgDao.create(siteMsg);
            return Boolean.TRUE;
        } catch (Exception e) {
            log.error("sendSiteMsg error", e);
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean sendSiteMsg(String[] receiverLoginIds, SiteMsg siteMsg) {
        Precondition.checkNotNull(receiverLoginIds, "receiverloginIds can not null!");
        Precondition.checkNotNull(siteMsg, "siteMsg can not null!");
        try {
            siteMsg.setSendDate(new Date());
            for (String receiverLoginId : receiverLoginIds) {
                siteMsg.setReceiverLoginId(receiverLoginId);
                siteMsgDao.create(siteMsg);
            }
            return Boolean.TRUE;
        } catch (Exception e) {
            log.error("sendSiteMsg error", e);
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean deleteSiteMsg(Long... ids) {
        Precondition.checkNotNull(ids, "id can not null!");
        // TODO 检索与卖场关联表,存在则抛异常
        try {
            for (Long id : ids) {
                siteMsgDao.delete(id);
            }
        } catch (Exception e) {
            log.error("deleteSiteMsg error", e);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public Page<SiteMsg> findAllByLoginId(String loginId, Integer start, Integer pageSize) {
        return siteMsgDao.findAllSiteMsg(loginId, start, pageSize);
    }

    public void setSiteMsgDao(SiteMsgDao siteMsgDao) {
        this.siteMsgDao = siteMsgDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
